#!/usr/bin/python
# coding=utf-8

from pwpd.common import config
from pwpd.core import pwdlogging
from pwpd.core import pwdcore
import Queue
import time

DEFULT_CONFIG = '/home/peter/workspace/eclipse_helios/pwpd_cert/etc/pwd_cert.conf'

def push_req_task(ppdetector, dname, pwsd_ll):
    ppdetector.push_task('http://' + dname)
    #
    for ll in pwsd_ll:
        url = 'http://' + dname + '/' + ll
        ppdetector.push_task(url)

def main():

    TT_Start = time.time()
    #
    pw_stat = 0    
    cofig_file = DEFULT_CONFIG
    # load config and initialize logger
    cfg = config.load_config(cofig_file)
    logfile = cfg.get_opt(config.OPT_LOGDIR) + '/' + 'pwpd.log'
    logger = pwdlogging.get_logger("pwd.daemon.cmdmain",logfile , default = True)
    
    # init. PhishingPageFeature & PhishingPageDetector
    ppfeature = pwdcore.PhishingPageFeature(cfg)
    ppdetector = pwdcore.PhishingPageDetector(cfg, ppfeature)
    logger.info('init. ppfeature & ppdetector done.')
    #
    try:
#        out_fd = open(cfg.get_opt(config.OPT_OUTPUT), 'w')
        out_fname = cfg.get_opt(config.OPT_OUTPUT) + '/' + time.strftime('%Y-%m-%d_', time.localtime()) + 'pwpd.out'
        out_fd = open(out_fname, 'w')
    except Exception, e:
        logger.exception(e)
        logger.fatal('cannot open output file: %s, cmdmain exit.', cfg.get_opt(config.OPT_OUTPUT))
        exit(1)    
    # try to construct all suspected urls. 
    pwsd_level = ppfeature.get_pwsd_level().items()
    pwsd_level.sort(cmp = lambda x, y : cmp(x[0], y[0]))
    pwsd_ll = pwdcore.construct_path(pwsd_level)
    logger.info('phishing resource path constructed: %d', len(pwsd_ll))

# =========================================
    raw_input_files = ppfeature.get_raw_input_files()
    for fname in raw_input_files:
        try:
            input_fd = open(fname, 'r')
        except Exception, e:
            logger.exception(e)
            logger.fatal('failed to open raw input file: %s, cmdmain exit', fname)
            exit(1)
        #
        dnames = 0
        for line in input_fd:
#            dname, ip_addr = line.strip().split('\t')
            dname = line.strip().split('\t')[0]           
            push_req_task(ppdetector, dname, pwsd_ll)
            dnames += 1
        # url push end
        logger.info('%s contains total suspected url: %d', fname, dnames)
        
    while ppdetector.left_task():
        item = ppdetector.pop_pw()
        if item:
            req, content = item
            print >> out_fd, '[%s]\t%s' %(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), req)
#            logger.debug('phishing page url:  %s; current number: %d', req, pw_stat)
            pw_stat += 1
        # 
        else:
            time.sleep(0.1)
    # END while
    out_fd.close()
    logger.info("total phishing pages detected: %d", pw_stat)

    TT_Finish = time.time()
    TT_elapse = TT_Finish - TT_Start 
    
    logger.info("clock elapse: %f", TT_elapse)
    logger.info('###################################### \n')
    print '=====>>>>> completed! Clock elapse: %f' % TT_elapse

# =========================================
    

if __name__ == '__main__':
    
    main()

    
    